package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.OrderCountQuery;
import com.isunland.managesystem.utils.ToastUtil;

public class SearchOrderCountFragment extends Fragment implements OnCheckedChangeListener {
  protected static final String EXTRA_VLAUE =
      "com.isunland.managesystem.ui.SearchOrderCountFragment.EXTRA_VLAUE";
  protected static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.SearchOrderCountFragment.EXTRA_TYPE";
  private CheckBox mcbProjectDeliveryCount;// 业务员
  private CheckBox mcbCustomerCompanyCount;//供应商/单位
  private CheckBox mcbTypeCount;// 物料名称
  private CheckBox mcbStorageCount;//所在仓库

  private OrderCountQuery content;
  private String statisticType = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    content = (OrderCountQuery) getArguments().getSerializable(EXTRA_VLAUE);
    statisticType = (String) getArguments().getSerializable(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

      case R.id.menu_item_confirm:
        if (TextUtils.isEmpty(content.getAppMan())
            && TextUtils.isEmpty(content.getCustomer())
            && TextUtils.isEmpty(content.getMaterial())
            && TextUtils.isEmpty(content.getStorage())) {
          ToastUtil.show(R.string.atLeastChooseOne);
        } else {
          volleyBackContent(content);
        }

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void volleyBackContent(OrderCountQuery content) {
    Intent i = new Intent();
    i.putExtra(OrderCountListFragment.EXTRA_QUERY, content);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_count_query, container, false);
    mcbProjectDeliveryCount = (CheckBox) view.findViewById(R.id.cb_projectDelivery_gatherPay);
    mcbCustomerCompanyCount = (CheckBox) view.findViewById(R.id.cb_customerCompany_gatherPay);
    mcbTypeCount = (CheckBox) view.findViewById(R.id.cb_typeCount_gatherPay);
    mcbStorageCount = (CheckBox) view.findViewById(R.id.cb_storage_gatherPay);

    if ("01".equals(statisticType)) {
      mcbCustomerCompanyCount.setText(R.string.customer_unit);
    } else if ("02".equals(statisticType)) {
      mcbCustomerCompanyCount.setText(R.string.supplier_unit);
    }

    if (!TextUtils.isEmpty(content.getAppMan())) {
      mcbProjectDeliveryCount.setChecked(true);
    } else {
      mcbProjectDeliveryCount.setChecked(false);
    }
    if (!TextUtils.isEmpty(content.getCustomer())) {
      mcbCustomerCompanyCount.setChecked(true);
    } else {
      mcbCustomerCompanyCount.setChecked(false);
    }
    if (!TextUtils.isEmpty(content.getMaterial())) {
      mcbTypeCount.setChecked(true);
    } else {
      mcbTypeCount.setChecked(false);
    }
    if (!TextUtils.isEmpty(content.getStorage())) {
      mcbStorageCount.setChecked(true);
    } else {
      mcbStorageCount.setChecked(false);
    }

    mcbProjectDeliveryCount.setOnCheckedChangeListener(this);
    mcbCustomerCompanyCount.setOnCheckedChangeListener(this);
    mcbTypeCount.setOnCheckedChangeListener(this);
    mcbStorageCount.setOnCheckedChangeListener(this);
    return view;
  }

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
      // 业务员
      case R.id.cb_projectDelivery_gatherPay:
        if (isChecked) {
          content.setAppMan("APP_MAN_ID,APP_MAN_NAME");
        } else {
          content.setAppMan("");
        }
        break;
      // 供应商/单位
      case R.id.cb_customerCompany_gatherPay:
        if (isChecked) {
          content.setCustomer("CUSTOMER_ID,CUSTOMER_NAME");
        } else {
          content.setCustomer("");
        }
        break;
      // 物料名称
      case R.id.cb_typeCount_gatherPay:
        if (isChecked) {
          content.setMaterial("MATERIAL_NAME");
        } else {
          content.setMaterial("");
        }
        break;
      // 所在仓库
      case R.id.cb_storage_gatherPay:
        if (isChecked) {
          content.setStorage("storage_name");
        } else {
          content.setStorage("");
        }
        break;
      default:
        break;
    }
  }

  public static SearchOrderCountFragment newInstence(OrderCountQuery content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VLAUE, content);
    SearchOrderCountFragment mfragment = new SearchOrderCountFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }

  public static SearchOrderCountFragment newInstence(OrderCountQuery content,
      String statisticType) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VLAUE, content);
    bundle.putSerializable(EXTRA_TYPE, statisticType);
    SearchOrderCountFragment mfragment = new SearchOrderCountFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
